<?php

namespace app\admin\library;

use app\admin\model\Admin;

use think\Cookie;
use think\Request;
use think\Session;


class Auth extends \bin\Auth
{
	protected $_time = 0;
	protected $requestUri = '';
	protected $breadcrumb = [];


	public function __construct()
	{
		parent::__construct();
		$this->_time = time();
	}

	public function __get($name)
	{
		return Session::get('admin.' . $name);
	}

	public function login($username,$password,$keeptime = 0)
	{
		$admin = Admin::get([ 'username' => $username ]);

		if(!$admin)
		{
			return false;
		}
		if($admin->password != md5(md5($password) . $admin->password_salt))
		{
			//错误次数++
			//Eg // $admin->loginfailure++;$admin->save();
			return false;
		}

		//$admin->loginfailure = 0;
		//$admin->token = Random::uuid();
		
		//设置上次登录 [current->last]
		$admin->last_time 	= $admin->current_time;
		$admin->last_ip 	= $admin->current_ip;
		//设置本次登录 [new]
		$admin->current_time	= $this->_time;
		$admin->current_ip		= \bin\Visitor::getIP();
		//其他参数设置
		$admin->login_hit++;
		//保存修改
		$admin->save();
		Session::set('admin',$admin);
		$this->keeplogin($keeptime);
		return true;
	}

	public function logout()
	{
		$admin = Admin::get(intval($this->id));
		if(!$admin)
		{
			return true;
		}
		$admin->token = '';
		$admin->save();
		Session::delete("admin");
		Cookie::delete("keeplogin");
		return true;
	}

	public function autoLogin()
	{
		$keeplogin = Cookie::get('keeplogin');
		if(!$keeplogin)
		{
			return false;
		}

		list($id,$keeptime,$expiretime,$key) = explode('|', $keeplogin);

		if($id && $keeptime && $expiretime && $key && $expiretime > time())
		{
			//keeplogin 有效
			$admin = Admin::get($id);
			if(!$admin || !$admin->token)
			{
				return false;
			}
			//token已经变更
			if ($key != md5(md5($id) . md5($keeptime) . md5($expiretime) . $admin->token))
			{
				return false;
			}
			Session::set("admin",$admin);
			//刷新自动登录的时间
			$this->keeplogin($keeptime);
			return true;
		}
		else
		{
			//cookie->keeplogin 无效
			return false;
		}
	}

	protected function keeplogin($keeptime = 0)
	{
		//刷新保存登录的cookie
		if($keeptime)
		{
			//过期时间 = 当前时间 + 保存时间
			$expiretime = $this->_time + $keeptime;
			$key = md5(md5($this->id) . md5($keeptime) . md5($expiretime) . $this->token);
			$data = [$this->id, $keeptime, $expiretime, $key];
			Cookie::set('keeplogin',implode('|', $data));
			return true;
		}
		return false;
	}

	public function check($name,$uid = '',$relation = 'or', $mode = 'url')
	{
		//方法归到静态方法
		return parent::check($name,$this->id,$relation,$mode);
	}



	public function match($array = [])
	{
		$request = Request::instance();
		//数组 字符串形式自动匹配
		$array = is_array($array) ? $array : explode(',',$arr);
		//如果自动匹配不出数组
		if(!$array)
		{
			return false;
		}

		//是否存在节点
		if (in_array(strtolower($request->action()), $array) || in_array('*', $array))
		{
			return true;
		}

		//没有找到匹配
		return false;
	}

	public function isLogin()
	{
		return Session::get('admin') ? true : false;
	}

	/**
	* 获取当前请求的URI
	* @return string
	*/
	public function getRequestUri()
	{
		return $this->requestUri;
	}

	/**
	* 设置当前请求的URI
	* @param string $uri
	*/
	public function setRequestUri($uri)
	{
		$this->requestUri = $uri;
	}

	public function getGroups($uid = null)
	{
		$uid = is_null($uid) ? $this->id : $uid;
		return parent::getGroups($uid);
	}

	public function getRuleList($uid = null)
	{
		$uid = is_null($uid) ? $this->id : $uid;
		return parent::getRuleList($uid);
	}

	public function getUserInfo($uid = null)
	{
		$uid = is_null($uid) ? $this->id : $uid;
		//intval 转为整数
		return $uid != $this->id ? Admin::get(intval($uid)) : Session::get('admin');
	}

	public function getRuleIds($uid = null)
	{
		$uid = is_null($uid) ? $this->id : $uid;
		return parent::getRuleIds($uid);
	}

	public function isSuperAdmin()
	{
		return in_array('*', $this->getRuleIds()) ? TRUE : FALSE;
	}

	public function getBreadCrumb($path = '')
	{
		if($this->breadcrumb || !$path)
			return $this->breadcrumb;

		$path_rule_id = 0;
		foreach ($this->rules as $rule)
		{
			$path_rule_id = $rule['name'] == $path ? $rule['id'] : $path_rule_id;
		}
		if($path_rule_id)
		{
			$this->breadcrumb = Tree::instance()->init($this->rules)->getParents($path_rule_id,true);
			foreach ($this->breadcrumb as $k => &$v)
			{
				$v['url'] = Url::build($v['name']);
			}
		}
		return $this->breadcrumb;
	}
}